# -*- coding: utf-8 -*-
# 定时任务
#
import tushare as ts
from sqlalchemy import create_engine
from gplucky.models import *
import datetime
from djangoProject import settings
import logging
import requests
from bs4 import BeautifulSoup
import re
import time

logger = logging.getLogger("django")
api = ts.pro_api(settings.TUSHARE_TOKEN)


class cron():
    global list_zjslYyb
    list_zjslYyb = set()

    def test(self):
        print('18888')


    # 实时行情（所有）
    # 每天先清空，再同步一次
    def syncTodayInfo(self):
        logger.info("syncTodayInfo任务开始………………")
        logger.info(settings.MYSQL_ENGINE_URL)
        try:
            logger.info("已获取engine……………………")
            if 0 != TsStockTodayInfo.objects.count():
                TsStockTodayInfo.objects.filter().delete()
            df = ts.get_today_all()
            df['create_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            engine = create_engine(settings.MYSQL_ENGINE_URL)
            # 存入数据库
            df.to_sql('ts_stock_today_info', engine, if_exists='append', index=False)
            df.to_sql('ts_stock_today_info_history', engine, if_exists='append', index=False)
        except Exception as e:
            logger.error("执行syncTodayInfo时异常, code=%s", e)
        logger.info("………………syncTodayInfo任务完成")

    def getCurrentInfoSingle(self, code):
        df = ts.get_realtime_quotes(code)  # Single stock symbol
        logger.info(df[['code', 'name', 'price', 'bid', 'ask', 'volume', 'amount', 'time']].loc[:'0', :])
        return df.to_html

    # 历史行情初始化(2017一年)
    def syncHistory(self, start_, end_):
        logger.info("syncHistory任务开始………………")
        try:
            logger.info("start=%s,end=%s" % (start_, end_))
            if start_ is None or end_ is None or str(start_).strip() == '':
                data_str = datetime.datetime.now().strftime('%Y-%m-%d')
                start_ = data_str
                end_ = data_str

            if 0 != TsStockHistory.objects.filter(date__range=(start_, end_)).count():
                TsStockHistory.objects.filter(date__range=(start_, end_)).delete()

            engine = create_engine(settings.MYSQL_ENGINE_URL)
            # 获取股票列表
            stock_list = TsStockTodayInfo.objects.all()
            for i, var in enumerate(stock_list):
                logger.info("%s,[%s][%s]" % (stock_list.count(), (i + 1), var.code))
                try:
                    # if start is None or end is None or str(start).strip() == '':
                    #     # data_str = datetime.datetime.now().strftime('%Y-%m-%d')
                    #     # start = data_str
                    #     # end = data_str
                    #     df1 = ts.get_k_data(var.code)
                    # else:
                    df1 = ts.get_k_data(var.code, start=start_, end=end_)
                    logger.info("%s" % df1)
                    df1['create_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    # 存入数据库
                    df1.to_sql('ts_stock_history', engine, if_exists='append', index=False)
                except Exception as e:
                    logger.error("do insert ts_stock_history Exception : %s", e)

                    # try:
                    #     # 旧方式
                    #     df2 = ts.get_hist_data(var.code, start, end)
                    #     df2['code'] = var.code
                    #     df2['create_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    #     # 存入数据库
                    #     df2.to_sql('ts_stock_history_old', engine, if_exists='append')
                    # except Exception, e:
                    #     logger.error("do insert ts_stock_history_old Exception : %s", e.message)
        except Exception as e:
            logger.error("执行syncHistory时异常:%s", e)
        logger.info("………………syncHistory任务完成")

    def getTodayAll(self):
        return ts.get_today_all().to_json(orient='records')

    def getInfoJson(self, code):
        return ts.get_realtime_quotes(code).to_json(orient='records')

    # 获取资金实力最强的营业部当天买入的股票集合
    def getZjslYyb(self, day):
        # 清空全局列表
        list_zjslYyb.clear()
        url_base = "http://data.10jqka.com.cn/ifmarket/lhbyyb/type/1/tab/zjsl/field/zgczje/sort/desc/page/"
        for num in range(2, (self.getTotalNumAndInitFirstData(url_base, day) + 1)):
            self.doFilterUrlAndGetCodeZjsl(BeautifulSoup(self.get_html_text(url_base + str(num)), 'lxml'), day)
        print(list(list_zjslYyb))
        return list(list_zjslYyb)

    def getTotalNumAndInitFirstData(self, url_base, day):
        try:
            html = self.get_html_text(url_base + "1")
            soup = BeautifulSoup(html, 'lxml')
            self.doFilterUrlAndGetCodeZjsl(soup, day)
            return int(str(soup.find("span", class_="page_info").text).split("/")[1])
        except Exception as e:
            logger.error("得到营业部总数时异常:%s", e)

    def doFilterUrlAndGetCodeZjsl(self, soup, day):
        if day is None:
            data_str = datetime.datetime.now().strftime('%Y-%m-%d')
            day = data_str
        # 解析营业部页面href
        for tag in soup.find_all(href=re.compile("lhbyyb")):
            try:
                # 获得营业部href主页
                yybHref = tag.attrs['href']
                # 获取此营业部最近一个交易日的上榜股票编码
                self.doGetCodeZjsl(yybHref, day)
            except Exception as e:
                logger.error("获取资金实力最强的营业部时异常:%s", e)
                continue

    # 获取此营业部最近一个交易日的上榜股票编码
    def doGetCodeZjsl(self, yybHref, day):
        # list_zjslYyb
        html = self.get_html_text(yybHref)
        soup = BeautifulSoup(html, 'lxml')
        for tag in soup.find_all("tr"):
            try:
                if tag.find_all("td")[0].text == day:
                    flg_a = (tag.find_all('td')[1]).find("a")
                    code = str(flg_a.attrs["href"]).split("/")[4]
                    name = flg_a.text
                    logger.info("yybHref=[%s],日期=[%s],代码=[%s],名称=[%s]" % (yybHref, day, code, name))
                    list_zjslYyb.add(code)
                else:
                    break
            except Exception as e:
                logger.error("获取营业部今日股票时异常:%s", e)
                continue

    def get_html_text(self, url):
        try:
            headers = {
                # 'Cookie': 'v=AncM6MWO4SzF-mWga6t7HQDjAGDFPEueJRDPEskkk8ateJkW0Qzb7jXgX2Pb',
                'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
            }
            r = requests.get(url, headers=headers)
            return r.text
        except Exception as e:
            logger.error("请求url=[%s],返回html=[]异常：%s" % (url, e))

    # 获取每日收盘行情
    def get_day_all(self, day):
        logger.info("获取%s收盘行情get_day_all任务开始………………", day)
        logger.info(settings.MYSQL_ENGINE_URL)
        try:
            logger.info("已获取engine……………………")
            df = ts.get_day_all(day)
            df['date'] = datetime.datetime.strptime(day, "%Y-%m-%d")
            df = df.rename(index=str, columns={"change": "changev", "range": "p_range"})
            engine = create_engine(settings.MYSQL_ENGINE_URL)
            # 存入数据库
            df.to_sql('get_day_all_history', engine, if_exists='append', index=False)
        except Exception as e:
            logger.error("%s执行get_day_all时异常, code=%s", day, e)
        logger.info("%s………………get_day_all任务完成", day)

    def new_stocks(self):
        logger.info("获取最新的新股行情new_stocks任务开始………………")
        logger.info(settings.MYSQL_ENGINE_URL)
        try:
            logger.info("已获取engine……………………")
            df = ts.new_stocks()
            df = df.rename(index=str, columns={"limit": "limit_v"})
            engine = create_engine(settings.MYSQL_ENGINE_URL)
            # 存入数据库
            df.to_sql('new_stocks', engine, if_exists='replace', index=False)
        except Exception as e:
            logger.error("执行new_stocks时异常, code=%s", e)
        logger.info("………………new_stocks任务完成")

    @classmethod
    def pro_bar_daily_ma(self, today_str):
        try:
            if today_str == '':
                today_str = datetime.datetime.now().strftime('%Y%m%d')
            stock_list = StockBasic.objects.all()

            StockDaily.objects.filter(trade_date=today_str).delete()

            engine = create_engine(settings.MYSQL_ENGINE_URL)

            num = 600;
            for i, var in enumerate(stock_list):
                try:
                    df = ts.pro_bar(api=api, ts_code=var.ts_code,
                                    start_date=(datetime.datetime.now() + datetime.timedelta(days=-500)).strftime('%Y%m%d'),
                                    end_date=today_str,
                                    factors=['tor', 'vr'],
                                    ma=[5, 10, 20, 30, 60, 90, 120, 250])
                    if df is None:
                        continue
                    if df.empty:
                        continue
                    df = df.rename(index=str, columns={"change": "changev", "pct_chg": "pct_change"})
                    #索引第一列
                    df2 = df[:1]
                    if df2['trade_date'][0] == today_str:
                        df2.to_sql('stock_daily', engine, if_exists='append', index=False)
                        logger.info(var.ts_code + " " + today_str)
                    num = num - 1;
                    if num <= 0 :
                        time.sleep(60);
                        num = 600;
                except Exception as e:
                    logger.error("code=[%s], error",var.ts_code, e)
                    continue

        except Exception as e:
            logger.error("执行pro_bar_daily_ma时异常", e)